
import React, { Component } from 'react'
import { Modal, Button, Input, Row, Col, Form, message, Upload } from 'antd';
import BaseComponent from '@/components/BaseComponent';
import { connect } from 'dva';
// import { saveAs } from 'file-saver';


@connect(({ user }) => ({
    // data: risk.riskData,
    currentUser: user.currentUser,
}))
export default class index extends BaseComponent {
    constructor(props) {
        super(props)
        this.state = {
            visible: false,
            uploadLoading: false,
        }
    }


    showModal = () => {
        this.setState({
            visible: true,
        });
    };

    handleOk = () => {
        this.setState({
            visible: false,
        });
    };

    handleCancel = e => {
        this.setState({
            visible: false,
        });
    };

    handleUploadBefore = (file, fileList) => {
        this.setState({ uploadLoading: true }, () => {
            this.hide = this.loadingMessage('正在上传...');
        });
        return true;
    };


    render() {

        const uploadProps = {
            name: 'file',
            action: `/api/cdcerserver/v1/cerpersoninfo/uploadFile4CerDataInit?user=${this.props.currentUser.id}`,
            // accept: '.xlsx,.xls',
            headers: {
                authorization: 'authorization-text',
            },
            onChange: info => {
                if (info.file.status !== 'uploading') {
                    this.setState({
                        fileList: info.fileList,
                    });
                }
                if (info.file.status === 'done') {
                    if (this.hide) {
                        this.hide();
                    }
                    this.setState({
                        uploadLoading: false,
                    });
                    this.successMessage('上传成功,请等待后台处理');
                    if(info.file.response.data!=null ){
                        const str = "http://wncloud.net:8001/personinit/" + info.file.response.data.errorFile
                        let a = document.createElement("a");
                        a.href = str;
                        a.click();
                    }
                    this.props.getDataByPage({})
                    this.handleOk()
                } else if (info.file.status === 'error') {
                    this.errorMessage('文件上传出错,请联系管理员');
                }
            },
        };
        return (
            <div style={{ display: "inline-block" }}>
                <Button type="primary" style={{ marginLeft: "10px" }} onClick={this.showModal}>
                    导入数据
                </Button>

                <Modal
                    title="导入数据下载"
                    visible={this.state.visible}
                    onOk={this.handleOk}
                    onCancel={this.handleCancel}
                >
                    <a href='https://wny-upload2.oss-cn-hangzhou.aliyuncs.com/nongai/%E4%BA%BA%E5%91%98%E4%BF%A1%E6%81%AF%E6%A8%A1%E6%9D%BF.xlsx'>下载导入表</a>
                    <br />
                    <Upload
                        {...uploadProps}
                        beforeUpload={this.handleUploadBefore}
                        showUploadList={false}
                        disabled={this.state.uploadLoading}
                    >
                        <Button type="primary" loading={this.state.uploadLoading}>
                            数据导入
                        </Button>
                    </Upload>
                </Modal>
            </div>
        )
    }
}
